<template>
  
  <view class="goods-item">
    <!-- 左侧的盒子1 -->
    <view class="goods-item-left">
      <radio  :checked="item.goods_state" color="#C00000" v-if="showRadio" @click="radioClickHandler"></radio>
      <image :src="item.goods_small_logo || defaultPic" mode="" class="goods-pic"></image>
    </view>
    <!-- 右侧的盒子 -->
    <view class="goods-item-right">
      <!-- 商品的名字 -->
      <view class="goods-name">{{item.goods_name}}</view>
      <view class="goods-info-box">
        <view class="goods-price">￥{{item.goods_price | tofixed}}</view>
        
        <uni-number-box :min="1" :value="item.goods_count" v-if="showNum" @change="numChangeHanlder"></uni-number-box>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    props:{
      item:{
        type: Object,
        default:{}
      },
      showRadio:{
        type:Boolean,
        // 默认情况下不会展示radio组件
        default: false
      },
      showNum:{
        type:Boolean,
        default: false
      }
    },
    name:"my-goods",
    data() {
      return {
        // 默认的图片
        defaultPic:'https://tse1-mm.cn.bing.net/th/id/R-C.901f8ebdab22d065baefeae6c2701cc0?rik=Z3Hew18zFaF%2bLQ&riu=http%3a%2f%2fwww.pp3.cn%2fuploads%2f20120418lw%2f13.jpg&ehk=Es5ZGH90h%2foCghvlIwdKfUiqpO05gLSgOEBU2i0Mwok%3d&risl=&pid=ImgRaw&r=0'
      };
    },
    filters:{
      tofixed(num){
        return Number(num).toFixed(2)
      }
    },
    methods:{
      // 这是radio组件的点击事件函数
      radioClickHandler(){
        this.$emit('radio-change',{
          goods_id : this.item.goods_id,
          goods_state : !this.item.goods_state
        })
      },
      // 这是监听到了numberBox的事件
      numChangeHanlder(val){
        // console.log(val);
        this.$emit('num-change',{
          goods_id:this.item.goods_id,
          goods_count: +val
        })
      }
    }
    
  }
</script>

<style lang="scss">
.goods-item{
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;
  width: 750rpx;
  box-sizing: border-box;
  .goods-item-left{
    margin-right: 5px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .goods-pic{
      width: 100px;
      height: 100px;
      display: block;
      
    }
  }
  .goods-item-right{
    display: flex;
    flex: 1;
    flex-direction: column;
    justify-content: space-between;
    .goods-name{
      font-size: 13px
    }
    .goods-info-box{
      display: flex;
      justify-content: space-between;
      align-items: center;
      
      .goods-price{
        color: #C00000;
        font-size: 16px
      }
    }
  }
}
</style>